﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Web;
using Yeap.Kernel.Utils.Extensions;

namespace Yeap.Kernel.Security
{
    public static class HashEngine
    {
        private static readonly HashAlgorithm Algorithm = SHA256.Create();

        public static String GenerateHash(String information, String salt)
        {
            byte[] buffer1 = Convert.FromBase64String(salt);
            byte[] buffer2 = buffer1.MergeByteArrays(information.ToByteArray());
            byte[] buffer3 = Algorithm.ComputeHash(buffer2);
            return Convert.ToBase64String(buffer3);
        }

        public static String GenerateSalt()
        {
            var buffer = new byte[0x10];
            RandomNumberGenerator.Create().GetBytes(buffer);
            return Convert.ToBase64String(buffer);
        }

        public static String GenerateSalt (String salt)
        {
            return Convert.ToBase64String(salt.ToByteArray());
        }

        public static bool IsValid(string password, string passwordHash, string salt)
        {
            var tempHash = GenerateHash(password, salt);
            return tempHash.EqualsIgnoreCase(passwordHash);
        }
    }
}